package com.jy.appmarket.cards;

import it.gmariotti.cardslib.library.internal.Card;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.jy.app.market.apk.type.CommentType;
import com.jy.app.market.idata.data.Apk;
import com.jy.app.market.idata.data.Comment;
import com.jy.app.market.idata.data.CommentList;
import com.jy.appmarket.R;
import com.jy.appmarket.activity.AppCommentActivity;
import com.jy.appmarket.utils.IdataUtils;
import com.jy.appmarket.utils.LoadImageUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * 类说明：
 * 
 * @author Baker.li
 * @date 2015年8月21日
 * @version 1.0
 */

public class AppDetailCommentUICard extends Card {
	private CommentList mComments;
	private Apk mApk;

	public AppDetailCommentUICard(Context context, CommentList ac, Apk apk) {
		super(context, R.layout.appdetail_comment_card_layout);
		mComments = ac;
		mApk = apk;
	}

	@Override
	public void setupInnerViewElements(ViewGroup parent, View view) {
		final View desc = view.findViewById(R.id.app_comment_discription);
		final View com1 = view.findViewById(R.id.app_comment_1);
		final View com2 = view.findViewById(R.id.app_comment_2);
		final View all = view.findViewById(R.id.app_comment_all);
		final View empty = view.findViewById(R.id.app_comment_empty);
		view.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent it = new Intent(getContext(), AppCommentActivity.class);
				it.putExtra(AppCommentActivity.EXTRA_NAME, mApk.getTitle());
				it.putExtra(AppCommentActivity.EXTRA_PKG, mApk.getPackageName());
				it.putExtra(AppCommentActivity.EXTRA_VERSIONCODE, mApk.getVersionCode());

				it.putExtra(AppCommentActivity.EXTRA_DLURL, mApk.getDownloadUrl().getUrl());
				it.putExtra(AppCommentActivity.EXTRA_ICONURL, IdataUtils.getIconUrl(mApk.getIcons()));
				it.putExtra(AppCommentActivity.EXTRA_VERSIONNAME, mApk.getVersionName());
				it.putExtra(AppCommentActivity.EXTRA_MD5, mApk.getMd5());
				it.putExtra(AppCommentActivity.EXTRA_FILESIZE, DownloadableUICard.safeInt(mApk.getBytes()));

				getContext().startActivity(it);
			}
		});
		TextView title = view.findViewById(R.id.app_comment_title);
		if (mComments == null || mComments.getComments() == null || mComments.getComments().size() == 0) {
			title.setText("应用评论（0）");
			desc.setVisibility(View.GONE);
			com1.setVisibility(View.GONE);
			com2.setVisibility(View.GONE);
			all.setVisibility(View.GONE);
			empty.setVisibility(View.VISIBLE);
			return;
		}

		desc.setVisibility(View.VISIBLE);
		all.setVisibility(View.VISIBLE);
		empty.setVisibility(View.GONE);
		int total = mComments.getGood() + mComments.getBad() + mComments.getOther();
		if (total == 0) {
			title.setText("应用评论（0）");
			total = Integer.MAX_VALUE;
		} else {
			title.setText("应用评论（" + total + "）");
		}

		TextView percent = view.findViewById(R.id.app_comment_percentage);
		int p = (int) (mComments.getGood() * 100.0 / total);
		percent.setText(String.valueOf(p));

		// ProgressBar likeRate = (ProgressBar)
		// view.findViewById(R.id.app_comment_like_progress);
		// TextView likeCount = (TextView)
		// view.findViewById(R.id.app_comment_like_count_text);
		// ProgressBar dislikeRate = (ProgressBar) view
		// .findViewById(R.id.app_comment_dislike_progress);
		// TextView dislikeCount = (TextView)
		// view.findViewById(R.id.app_comment_dislike_count_text);
		// final int like = (int) (mComments.getGood() * 1000.0 / total);
		// likeRate.setProgress(like);
		// likeCount.setText(mComments.getGood() + "好评");
		// final int dislike = (int) (mComments.getBad() * 1000.0 / total);
		// dislikeRate.setProgress(dislike);
		// dislikeCount.setText(mComments.getBad() + "差评");

		// showLikeDisLiskProgress(view, 1000,10);
		showLikeDisLiskProgress(view, mComments.getGood(), mComments.getBad());

		com1.setVisibility(View.VISIBLE);
		List<Comment> list = mComments.getComments();
		setComment(com1, list.get(0));

		if (list.size() > 1) {
			com2.setVisibility(View.VISIBLE);
			setComment(com2, list.get(1));
		} else {
			com2.setVisibility(View.GONE);
		}

	}

	private void showLikeDisLiskProgress(View parent, int good, int bad) {
		ProgressBar likeRate = parent.findViewById(R.id.app_comment_like_progress);
		TextView likeCount = parent.findViewById(R.id.app_comment_like_count_text);

		ProgressBar dislikeRate = parent.findViewById(R.id.app_comment_dislike_progress);
		TextView dislikeCount = parent.findViewById(R.id.app_comment_dislike_count_text);

		int bigger = good > bad ? good : bad;
		if (bigger <= 200) {
			likeRate.setMax(200);
			dislikeRate.setMax(200);
		} else if (bigger <= 500) {
			likeRate.setMax(500);
			dislikeRate.setMax(500);
		} else if (bigger <= 1000) {
			likeRate.setMax(1000);
			dislikeRate.setMax(1000);
		} else {
			likeRate.setMax(bigger);
			dislikeRate.setMax(bigger);
		}

		likeRate.setProgress(good + 1);
		dislikeRate.setProgress(bad + 1);

		likeCount.setText(good + "好评");
		dislikeCount.setText(bad + "差评");
	}

	private void setComment(View parent, Comment comment) {
		final ImageView icon = parent.findViewById(R.id.user_icon);
		final TextView name = parent.findViewById(R.id.author_name);
		final TextView date = parent.findViewById(R.id.date);
		final TextView content = parent.findViewById(R.id.content);
		final ImageView heart = parent.findViewById(R.id.heart);

		ImageLoader.getInstance().displayImage(comment.getIconUrl(), icon,
				LoadImageUtils.getListIconDisplayOption(mContext));
		name.setText(comment.getNickName());
		date.setText(comment.getTime());
		content.setText(comment.getContent());

		if (CommentType.BAD == comment.getEvaluate()) {
			heart.setImageResource(R.drawable.icon_dislike_f);
		} else if (CommentType.GOOD == comment.getEvaluate()) {
			heart.setImageResource(R.drawable.icon_like_f);
		} else {
			heart.setImageResource(R.drawable.transparent);
		}
	}

	@Override
	public int getType() {
		return 6;
	}

	@Override
	public int getMarginTop() {
		return 0; // 返回0，则在顶部没有空隙
	}
}